import os
import re
import shutil

import openpyxl


def move():
    listdir = os.listdir('D:/数禾-1175件委案图文资料-密码是：04t$9JLD2x/')
    for file_dir in listdir:
        print("----------------------------------------------------------------------------------------------")
        print("处理外层文件夹" + file_dir)
        new_dir = 'D:/数禾-1175件委案图文资料-密码是：04t$9JLD2x/' + file_dir
        dictionary_dir = os.listdir(new_dir)
        for ff_dir in dictionary_dir:
            print("处理内层文件" + ff_dir)
            pattern = '[A-Z]{2}[0-9]{16}\.'
            match = re.search(pattern, ff_dir)
            if match:
                search = match.span()
                f_new_dir = 'D:/test1/' + ff_dir[search[0]:search[1] - 2]
                if os.path.exists(f_new_dir):
                    print("转移成功**********************************" + f_new_dir + "****" + ff_dir)
                    new_file = f_new_dir + "/" + ff_dir
                    shutil.move("D:/数禾-1175件委案图文资料-密码是：04t$9JLD2x/" + file_dir + "/" + ff_dir, new_file)


def move2():
    workbook = openpyxl.load_workbook('D:/Documents/数禾/资产处置委案明细20230403163249逸度5589.xlsx')
    sheets = workbook.worksheets[0]
    rows = sheets.rows
    for row in rows:
        print(row[0].value)
        out_folder_dir = 'D:/Documents/数禾/数禾-调解资料包/调解/' + str(row[0].value)
        if os.path.exists(out_folder_dir):
            out_list_dir = os.listdir(out_folder_dir)
            for scr_file_dir in out_list_dir:
                if 'ZHonGHEXIN' == row[21].value:
                    put_folder = 'D:/Documents/数禾/新分类/ZHonGHEXIN/' + str(row[0].value) + '/'
                    if os.path.exists(put_folder):
                        print("")
                    else:
                        os.makedirs(put_folder)
                    shutil.move('D:/Documents/数禾/数禾-调解资料包/调解/' + str(row[0].value) + '/' + scr_file_dir,
                                put_folder + scr_file_dir)
                elif 'SHUHE' == row[21].value:
                    put_folder = 'D:/Documents/数禾/新分类/SHUHE/' + str(row[0].value) + '/'
                    if os.path.exists(put_folder):
                        print("")
                    else:
                        os.makedirs(put_folder)
                    shutil.move('D:/Documents/数禾/数禾-调解资料包/调解/' + str(row[0].value) + '/' + scr_file_dir,
                                put_folder + scr_file_dir)


def make():
    workbook = openpyxl.load_workbook('D:/更新版-数禾-1175件立案明细.xlsx')
    sheets = workbook.worksheets[0]
    rows = sheets.rows
    for row in rows:
        os.makedirs('D:/test1/' + str(row[3].value) + '')


def search():
    order_ids = []
    workbook = openpyxl.load_workbook('D:/萨摩耶-《案件信息表》逸度科技20230406 750.xlsx')
    sheets = workbook.worksheets[0]
    rows = sheets.rows
    for row in rows:
        order_ids.append(row[1].value)

    src_folder = 'D:/萨摩耶-20230406195849_材料包/萨摩耶-20230406195849_材料包'
    listdir = os.listdir(src_folder)
    pattern = '[C]{1}[0-9]{18}'
    for file_dir in listdir:
        match = re.search(pattern, file_dir)
        if match:
            span = match.span()
            order_id = file_dir[span[0]:]
            count = order_ids.count(order_id)
            if count < 1:
                print(file_dir)


if __name__ == '__main__':
    move2()
